//
//  LTClassReportListView.m
//  LearnTeacher
//
//  Created by zyl on 2024/4/12.
//

#import "LTClassReportListView.h"

@implementation LTClassReportListView

#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@""];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        cell.backgroundColor = rgba(244, 245, 247, 1);
    }
    
    [cell removeAllSubviews];
    NSArray *array = self.data[indexPath.row];
    for (int i = 0; i < array.count; i++) {
        UILabel *titlelab = self.titleLabels[i];
        UILabel *contentLab = [[UILabel alloc] initWithFrame:titlelab.frame];
        contentLab.height = 28;
        contentLab.font = RFontWithSize(13);
        contentLab.text = [NSString stringWithFormat:@"%@",array[i]];
        [contentLab setTextAlignment:NSTextAlignmentCenter];
        contentLab.textColor = LTTextBlack;
        [cell addSubview:contentLab];
    }
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.data.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    return 28;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 34;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] init];
    
    headerView.frame = CGRectMake(0,0,tableView.width,34);
    headerView.backgroundColor = LTWhiteColor;
    for (int i = 0; i < self.titleLabels.count; i++) {
        UILabel *titlelab = self.titleLabels[i];
        [headerView addSubview:titlelab];
        if (i == 0) {
            [titlelab setCorners:UIRectCornerTopLeft cornerRadius:8];
        }
        if (i + 1 == self.titleLabels.count) {
            [titlelab setCorners:UIRectCornerTopRight cornerRadius:8];
        }
    }
    return headerView;
}
- (void)setTitles:(NSArray *)titles
{
    _titles = titles;
    
    self.titleLabels = @[].mutableCopy;
    double x = 0;
    for (int i = 0; i < self.titles.count; i++) {
        //
        double w = [MethodTool getTextWidthWithStr:self.titles[i] font:SFontWithSize(14) ViewHeight:32];
        w = i == 0? w + 34:w + 24;
        UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(x, 0, w, 32)];
        x = x + w + 1;
        titleLab.font = SFontWithSize(14);
        titleLab.text = self.titles[i];
        [titleLab setTextAlignment:NSTextAlignmentCenter];
        titleLab.textColor = LTWhiteColor;
        titleLab.backgroundColor = rgba(122, 167, 255, 1);
        [self.titleLabels addObject:titleLab];
        self.tabWidth.constant = titleLab.x + titleLab.width;
    }
    [self.tableView reloadData];
    
}
- (void)setData:(NSArray *)data
{
    _data = data;
    self.tabHeight.constant = self.data.count*28 + 34;
    //self.scrollHeight.constant = self.tabHeight.constant;
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    } else {
        // Fallback on earlier versions
    }
    [self.tableView reloadData];
}
@end
